import { runCommand } from ".";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        runCommand(context, options, isUndo, (sheet, options) => {
            const { selections, cellSpan } = options;
            selections.forEach(range => {
                if (range.rowCount == -1) {
                    range.rowCount = sheet.getRowCount();
                }
                if (range.colCount == -1) {
                    range.colCount = sheet.getColumnCount()
                }

                range.forEach((row, col) => {
                    if (row !== range.row || col !== range.col) {
                        const table = sheet.tables.find(row, col);
                        let cell;
                        if (table) {
                            const { row:tableRow, col: tableCol } = table.dataRange();
                            cell = sheet.getCell(tableRow, col);
                            table.setColumnDataField(col - tableCol, undefined);
                        } else {
                            cell = sheet.getCell(row, col);
                        }
                        cell.cellType(undefined)
                            .backColor(undefined)
                            .textIndent(undefined)
                            .backgroundImageLayout(undefined)
                            .backgroundImage(undefined);
                    }
                })
            });
            if (cellSpan) {
                selections.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });
            } else {
                selections.forEach(range=>{
                    range.forEach((row, col)=>{
                        const span = sheet.getSpan(row, col);
                        if(span) {
                            sheet.removeSpan(span.row, span.col);
                        }
                        
                    })
                })
            }
        })
    }
};

/* export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheet, selections, _oldSpans = [], cellSpan } = options;
        try {
            sheet.suspendPaint();
            if (isUndo) {
                if (cellSpan) {
                    selections.forEach(detail => {
                        sheet.removeSpan(detail.row, detail.col);
                        for (let row = detail.row, i = 0; i < detail.rowCount; i++ , row++) {
                            for (let col = detail.col, j = 0; j < detail.colCount; col++ , j++) {
                                if (i == 0 && j == 0) {
                                    continue;
                                }
                                let cell;
                                const table = sheet.tables.find(row, col);
                                if (table) {
                                    // continue;
                                    cell = sheet.getCell(table.dataRange().row, col);
                                } else {
                                    cell = sheet.getCell(row, col);
                                }
                                cell.cellType(options._oldCellTypes[row][col])
                                    .backColor(options._oldBackColor[row][col])
                                    .textIndent(options._oldTextIndent[row][col])
                                    .backgroundImageLayout(options._oldBackgroundImageLayout[row][col])
                                    .backgroundImage(options._oldBackGroundImage[row][col]);
                            }
                        }
                    });
                }

                _oldSpans.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });


            } else {
                options._oldBackColor = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                selections.forEach(range => {
                    if (range.rowCount == -1) {
                        range.rowCount = sheet.getRowCount();
                    }
                    if (range.colCount == -1) {
                        range.colCount = sheet.getColumnCount()
                    }
                    for (let row = range.row, i = 0; i < range.rowCount; i++ , row++) {
                        if (!options._oldBackColor[row]) {
                            options._oldBackColor[row] = {}
                        }
                        if (!options._oldCellTypes[row]) {
                            options._oldCellTypes[row] = {};
                        }
                        if (!options._oldTextIndent[row]) {
                            options._oldTextIndent[row] = {};
                        }
                        if (!options._oldBackGroundImage[row]) {
                            options._oldBackGroundImage[row] = {};
                        }
                        if (!options._oldBackgroundImageLayout[row]) {
                            options._oldBackgroundImageLayout[row] = {};
                        }
                        for (let col = range.col, j = 0; j < range.colCount; j++ , col++) {

                            if (i != 0 || j != 0) {
                                let cell;
                                const table = sheet.tables.find(row, col);
                                if (table) {
                                    // continue;
                                    cell = sheet.getCell(table.dataRange().row, col);
                                } else {
                                    cell = sheet.getCell(row, col);
                                }
                                options._oldBackColor[row][col] = cell.backColor();
                                options._oldCellTypes[row][col] = cell.cellType();
                                options._oldTextIndent[row][col] = cell.textIndent();
                                options._oldBackGroundImage[row][col] = cell.backgroundImage();
                                options._oldBackgroundImageLayout[row][col] = cell.backgroundImageLayout();

                                cell.cellType(undefined)
                                    .backColor(undefined)
                                    .textIndent(undefined)
                                    .backgroundImageLayout(undefined)
                                    .backgroundImage(undefined);

                            }
                            const span = sheet.getSpan(row, col);
                            if (span && span.row === row && span.col === col) {
                                if (!options._oldSpans) {
                                    options._oldSpans = [];
                                }
                                options._oldSpans.push(span);
                            }
                            if (row !== range.row || col !== range.col) {
                                sheet.getCell(row, col).cellType(undefined);
                                const table = sheet.tables.find(row, col);
                                if (table) {
                                    const { col: tableCol } = table.range();
                                    table.setColumnDataField(col - tableCol, undefined);
                                }
                            }
                        }
                    }
                });
                if (cellSpan) {
                    selections.forEach(range => {
                        sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);



                    });
                } else {
                    options._oldSpans.forEach(span => {
                        sheet.removeSpan(span.row, span.col);
                    });
                }
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
}; */